package com.tianqu.tms.www.rental.action;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.rental.actionform.RentalLocationAddInputForm;
import com.tianqu.tms.www.rental.viewhelper.RentalLocationViewHelper;

/**
 * 合同查询/修改-高级搜索 action
 * @author mufeiyu
 *
 */
public class RentalLocationUpdateHistroySearchInputAction extends TmsAction {

	
	private Logger logger = Logger.getLogger(RentalLocationUpdateHistroySearchInputAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		//去掉缓存
		if(uc.getAttribute("seniorForm")!=null)
		{
			uc.removeAttribute("seniorForm");
		}
		
		RentalLocationViewHelper viewHelper = new RentalLocationViewHelper();
		String searchUpdateHistroyButton = request.getParameter("searchUpdateHistroyButton");
		
		// 初次
		if (StringUtil.isNullOrBlank(searchUpdateHistroyButton)) {
			
			// 操作类型
			Codes codes = Codes.getInstance();
			List handleTypeList = codes.getCodeslistByItemId(Codes.RENTAL_UPDATE_TYPE);

			for (Object object : handleTypeList) {
				String checked = "checked";
				HashMap codesMap = (HashMap)object;
				codesMap.put("checked", checked);
			}
			
			viewHelper.setLocationSerial("");
			viewHelper.setHandleName("");
			viewHelper.setHandleType("");
			viewHelper.setHandleTypeList(handleTypeList);
			viewHelper.setHandleDateS("");
			viewHelper.setHandleDateE("");
		} else {
		
			// Controller验证失败
			if("true".equals(uc.getAttribute("errFlg"))){
				viewHelper = (RentalLocationViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);

				uc.removeAttribute(TmsConstants.VIEWHELP);
				uc.removeAttribute("errFlg");
				uc.setAttribute("searchFlg", "2");
			} else if (!StringUtil.isNullOrBlank(searchUpdateHistroyButton) && "1".equals(searchUpdateHistroyButton)){
				// validation-rental.xml验证失败				
				RentalLocationAddInputForm rentalForm = (RentalLocationAddInputForm) uc.getInputForm();
				
				viewHelper.setLocationSerial(rentalForm.getLocationSerial());
				viewHelper.setHandleName(rentalForm.getHandleName());
				viewHelper.setHandleTypeList(converthandleType(rentalForm.getHandleType()));
				viewHelper.setHandleDateS(rentalForm.getHandleDateS());
				viewHelper.setHandleDateE(rentalForm.getHandleDateE());
			}	
		}
		
		uc.setAttribute(TmsConstants.VIEWHELP, viewHelper);

		uc.setAttribute("searchFlg", "2");
		request.setAttribute(TmsConstants.VIEWHELP, viewHelper);		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}

	private List converthandleType(String[] handleType) throws TmsDaoException{
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		if(null != handleType && handleType.length > 0){
			for (int i = 0; i < handleType.length; i++){
				int itemCode = StringUtils.isNumeric(handleType[i])?Integer.parseInt(handleType[i]):-1;
				map.put(itemCode, itemCode);
			}
		}
		List codeslist = Codes.getInstance().getCodeslistByItemId(Codes.RENTAL_UPDATE_TYPE);
		for (Object object : codeslist) {
			String checked = "";
			HashMap codesMap = (HashMap)object;
			if(map.get(codesMap.get("itemCode"))!=null ) checked = "checked";
			codesMap.put("checked", checked);
		}
		return codeslist;
	}
}